using System;
using System.IO;
using UnityEngine;
using LS.Digitize;

namespace LS.Digitize
{
    public sealed class ColorSerializer: CompoundSerializer<Color>
    {
        public ColorSerializer() : base(typeof(Color),typeof(float))
        {
            
        }
        
        public override void WriteData(IWriter writer, Color obj)
        {
            ISerializer subSerializer = SubSerializer;
            using (writer.WriteObjectScope())
            {
                if (subSerializer is PrimitiveSerializer<float> ps)
                {
                    using (writer.WritePropertyScope("r"))
                        ps.WriteData(writer, obj.r);
                    
                    using (writer.WritePropertyScope("g"))
                        ps.WriteData(writer, obj.g);
                    
                    using (writer.WritePropertyScope("b"))
                        ps.WriteData(writer, obj.b);
                    
                    using (writer.WritePropertyScope("a"))
                        ps.WriteData(writer, obj.a);
                }
                else
                {
                    using (writer.WritePropertyScope("r"))
                        subSerializer.Write(writer, obj.r);
                    
                    using (writer.WritePropertyScope("g"))
                        subSerializer.Write(writer, obj.g);
                    
                    using (writer.WritePropertyScope("b"))
                        subSerializer.Write(writer, obj.b);
                    
                    using (writer.WritePropertyScope("a"))
                        subSerializer.Write(writer, obj.a);
                }
            }
        }

        public override Color ReadData(IReader reader)
        {
            ISerializer subSerializer = SubSerializer;
            Color target = default;
            using (var objScope = reader.ReadObjectScope())
            {
                string propertyName = null;
                if (subSerializer is PrimitiveSerializer<float> ps)
                {
                    propertyName = reader.ReadPropertyName();
                    float r = ps.ReadData(reader);
                    propertyName = reader.ReadPropertyName();
                    float g = ps.ReadData(reader);
                    propertyName = reader.ReadPropertyName();
                    float b = ps.ReadData(reader);
                    propertyName = reader.ReadPropertyName();
                    float a = ps.ReadData(reader);
                    target = new Color(r, g, b, a);
                }
                else
                {
                    propertyName = reader.ReadPropertyName();
                    float r = (float)subSerializer.Read(reader);
                    propertyName = reader.ReadPropertyName();
                    float g = (float)subSerializer.Read(reader);
                    propertyName = reader.ReadPropertyName();
                    float b = (float)subSerializer.Read(reader);
                    propertyName = reader.ReadPropertyName();
                    float a = (float)subSerializer.Read(reader);
                    target = new Color(r, g, b, a);
                }
            }

            return target;
        }
        
        public override void ReadInto(IReader reader, ref Color obj)
        {
            obj = ReadData(reader);
        }
    }
}